﻿var main = null;
$(function () {
    main = new Main();
    main.init();

    setInterval(function(){
        if($("#checkbox_autoRefresh").attr("checked")){
            main.dg.query()
        }
    },5000);
});
//主操作类
function Main() {
   
    this.init = function () {
        this.dg = new Dg();
        this.dg.init();

        this.dlg = new Dlg();
        this.dlg.init();

        $("#d_tb .btn_add").click(function () {
            main.dlg.showAdd();
        });
        $("#d_tb .btn_modify").click(function () {
            main.dlg.showModify();
        });
        $("#d_tb .btn_del").click(function () {
            main.dg.del();
        });
    };
}
//主表格类,继承Dg_Method的方法
function Dg() {
    this.url = "/Cs_ScanRecord";
    this.init = Dg_Method.init;
    this.onDblClickRow = function(){};
    this.getColumns = function () {
        var columns = new Array();
        columns.push(
            { field: "id", title: "ID", hidden: false, checkbox: true },
            { field: "ip", title: "ip", width: 200 },
            { field:"scanPrjName",title:"扫描项目",width:200},
            { field: "flag", title: "是否告警", width: 200,formatter:function(val,src){
                switch (val){
                    case "hasWarning":
                        return "<strong style='color:red'>有告警</strong>";
                    case "noWarning":
                        return "无告警";
                    case "error":
                        return "错误";
                }
            } },
            {
                field: "result", title: "内容", width: 200, formatter: function (val, src) {
                    var mainResult = val;
                    if(src.mainResult){
                        mainResult = src.mainResult;
                    }
                    return "<span title='" + val + "'>" + mainResult + "</span>";
                }
            },
            { field: "createTime", title: "时间", width: 200 }
        );
        return columns;
    };
}
//主对话框类,继承Dg_Method的方法
function Dlg() {
    this.url = "/Cs_ScanRecord";
    this.init = Dlg_Method.init;
}
function clearAllRecord() {
    $.messager.confirm("确认删除", "确认要删除所有记录吗", function (r) {
        if (r) {
            $("#btn_clearAllRecord").showLoading();
            $.post("/Cs_ScanRecord",{action:"delAll"},function(result){
                if(result.success){
                    $.messager.show({title:"成功",msg:"清理记录成功"});
                }else{
                    $.messager.alert("出错",result.msg,"error");
                }
            },"json").complete(function(){
                $("#btn_clearAllRecord").hideLoading();
                main.dg.query();

            }).error(onError);

        }
    });
}
//重置扫描
function resetScan(){
    $.messager.confirm("确认重置扫描", "此操作将会清空所有扫描记录,所有扫描任务", function (r) {
        if(r){
            $.messager.progress();
            $.post("/ConstantlyScan",{action:"resetScan"},function(result){
                if(result.success){
                    $.messager.show({title:"成功",msg:"重置扫描成功"});
                }else{
                    $.messager.alert("失败",result.msg,"error");
                }
            },"json").complete(function(){
                $.messager.progress("close");
                main.dg.query();
            }).error(onError);
        }
    });
}


function addIp(){
    $("#addIp_submit").focus();
    var ip = $("#addIp_ip").textbox("getValue");
    $("#addIp_submit").showLoading();
    $.post("/Cs_Ipxx",{action:"add",ip:ip},function(result){
        if(result.success){
            $("#addIp_ip").textbox("setValue","");
            $.messager.show({
                title: "成功",
                msg: "添加ip:"+ip+" 成功"
            });
        }else{
            $.messager.alert("失败",result.msg,"error");
        }

    },"json").complete(function(){
        $("#addIp_submit").hideLoading();
    }).error(onError);

}


